/*
 * StringRecord.cpp
 *
 */

#include "StringRecord.h"

ById StringRecord::evalById;
ByValue StringRecord::evalByValue;

StringRecord::StringRecord() {
	id = 0;
	value = "";
}

StringRecord::StringRecord(const StringRecord& copy) {
	id = copy.id;
	value = copy.value;
}

StringRecord::StringRecord(BigInt id, const String& value) {
	this->id = id;
	this->value = value;
}

StringRecord::~StringRecord() {}

StringRecord& StringRecord::operator =(const StringRecord& other) {
	id = other.id;
	value = other.value;
	return *this;
}

BigInt StringRecord::getId() const {
	return id;
}

const String& StringRecord::getValue() const {
	return value;
}

void StringRecord::setId(BigInt id) {
	this->id = id;
}

void StringRecord::setValue(const String& value) {
	this->value = value;
}

BigInt StringRecord::size() const {
	return 4 + 1 + value.size();
}

void StringRecord::serialize(ISerializer& serializer) const {
	serializer.serializeUnsignedLongInt(id);
	serializer.serializeString(value);
}

void StringRecord::unserialize(IUnserializer& unserializer) {
	id = unserializer.unserializeUnsignedLongInt();
	value = unserializer.unserializeString();
}

int ById::eval(const StringRecord& record) const {
	return record.getId() - id;
}

int ByValue::eval(const StringRecord& record) const {
	return record.getValue().compare(value);
}

const ById& StringRecord::byId(BigInt id) {
	evalById.id = id;
	return evalById;
}

const ByValue& StringRecord::byValue(const String& value) {
	evalByValue.value = value;
	return evalByValue;
}


bool StringRecord::compByIdDesc (void* a, void* b) {
	return ((StringRecord*)a)->getId() < ((StringRecord*)b)->getId();
}

bool StringRecord::compByValDesc (void* a, void* b) {
	return ((StringRecord*)a)->getValue() < ((StringRecord*)b)->getValue();
}

std::ostream& operator<< (std::ostream& out, const StringRecord& record) {
	return out << record.getId() << ", " << record.getValue();
}
